package com.xingfly.service;

import com.xingfly.model.Event;
import com.xingfly.model.Following;
import com.xingfly.model.Post;
import com.xingfly.model.Relation;
import com.xingfly.repository.EventRepository;
import com.xingfly.repository.EventUserRepository;
import com.xingfly.util.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 事件服务
 * Created by SuperS on 2016/12/8.
 */
@Service
public class EventService {
    private final EventRepository eventRepository;
    private final EventUserRepository eventUserRepository;

    @Autowired
    public EventService(EventRepository eventRepository, EventUserRepository eventUserRepository) {
        this.eventRepository = eventRepository;
        this.eventUserRepository = eventUserRepository;
    }

    /**
     * 将object转换为事件
     */
    private Event toEvent(int objectType, Object object) {
        Event event = new Event();
        if (Type.OBJECT_TYPE_POST == objectType) {
            Post post = (Post) object;
            event.setObjectType(Type.OBJECT_TYPE_POST);
            event.setObjectId(post.getId());
            event.setUser(post.getAuthor());
            event.setTitle(post.getTitle());
            event.setSummary(post.getExcerpt());
            event.setContent(post.getContent());
            event.setLikeCount(post.getLikeCount());
            event.setShareCount(post.getShareCount());
            event.setCommentCount(post.getCommentCount());
        }
        if(Type.OBJECT_TYPE_FOLLOWING==objectType){
            Following following = (Following)object;
            event.setObjectType(Type.OBJECT_TYPE_FOLLOWING);
            event.setObjectId(following.getId());
            event.setUser(following.getUser());
            event.setTitle("关注操作");
            event.setSummary(following.getUser().getNickname()+"关注了"+following.getFollowingUser().getNickname());
            event.setContent(following.getUser().getNickname()+"关注了"+following.getFollowingUser().getNickname());
        }
        return event;
    }

    /**
     * 创建事件
     *
     * @param objectType 对象类型
     * @param obj        对象
     * @return Event 事件
     */
    public Event create(int objectType, Object obj) {
        return eventRepository.save(toEvent(objectType, obj));
    }

    /**
     * 分页获取事件列表
     *
     * @param start 从第几个
     * @param step  向后多少个
     * @return list 事件列表
     */
    public List<Event> getEvents(int start, int step) {
        return eventRepository.findAll(new PageRequest(start, step)).getContent();
    }

    /**
     * TODO 可能有问题
     * 根据Relation（objectType,objectId）查询Event列表
     *
     * @param relations 关系列表
     * @return events
     */
    public List<Event> getEventsWithRelations(List<Relation> relations) {
        HashSet<Event> events = new HashSet<>();
        for (Relation relation : relations) {
            events.addAll(eventRepository.getEvensByObjectTypeAndObjectId(relation.getObjectType(), relation.getObjectId()));
        }
        List<Event> eventList = new ArrayList<>();
        eventList.addAll(events);
        return eventList;
    }

    /**
     * 通过用户ID获取Events列表 可以通过count设置数量
     *
     * @param userId 用户id
     * @param count  列表大小
     */
    public List<Event> getEventsByUserId(Long userId, int count) {
        return eventRepository.findByUserId(userId, new PageRequest(0, count));
    }

    /**
     * 根据对象类型和对象id获取event
     *
     * @param id   对象id
     * @param type 对象类型
     */
    public Event getEvent(int type, Long id) {
        return eventRepository.find(type, id);
    }

    /**
     * 根据id删除event
     *
     * @param id id
     */
    public void delete(Long id) {
        eventRepository.delete(id);
    }

    /**
     * 根据objectType和objectID删除event
     *
     * @param type 对象类型
     * @param id   对象id
     */
    public void delete(int type, Long id) {
        eventRepository.deleteByObjectTypeAndObjectId(type, id);
    }
    /**
     * 删除事件
     * */
    public void delete(Event event){
        eventRepository.delete(event);
    }

    /**
     * 根据EventIds获取Event列表
     */
    public List<Event> getEventsWithIDs(List<Long> eventIds) {
        return eventRepository.findEventsByIds(eventIds);
    }
}
